<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>账号注册</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body>


<div id="loginFormDiv">
    <!-- 你的 HTML 代码 -->

    <form class="layui-form" action="" lay-filter="example" id="loginForm">
        <h3 style="text-align: center; padding-bottom: 30px"> 用户注册 </h3>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入用户名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="real_name" placeholder="请输入名字" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="0" title="男" checked="">
                <input type="radio" name="sex" value="1" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="exampleInputFile">上传头像</label>
            <input type="file" id="exampleInputFile" name="source">
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="loginFormBtn">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>

            </div>
        </div>


    </form>
    <button id="login_btn" class="layui-btn" style="margin-left: 110px">已有账号 去登录</button>
</div>

<script src="./layui/layui.js"></script>
<script>

    // layui框架的核心函数 layui.use(); 相当于 jquery的 $();
    layui.use(
        function () {
            // 代码起始位置

            var layer = layui.layer
                , form = layui.form
                , $ = layui.$
                , laydate = layui.laydate;

            //日期
            laydate.render({
                elem: '#date'
            });


            // layer.msg('Hello World');

            //监听提交
            form.on('submit(loginFormBtn)', function (data) {
                // layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // });
                // 获取表单数据，包含文件内容
                var formData = new FormData(document.getElementById("loginForm"));

                // 发送ajxa请求
                $.ajax({
                    url: "/user/regist",
                    type: "post",
                    data: formData,
                    processData:false,
                    contentType:false,  // 防止jquery在提交时把 boundary 分隔符取消
                    success: function (res) {
                        console.log(res);
                        if (res.code ==="1004"){
                            //注册成功
                            layer.msg(res.msg);
                            setTimeout( function(){
                                window.location.href="/login.html"
                            }, 2000 );//延迟5000毫秒
                        }else {
                            layer.msg(res.msg);
                        }

                    },
                    error:function (result) {
                        console.log(result);
                    }
                });

                return false; // 取消表单的默认提交行为
            });


            $("#login_btn").click(function () {
                window.location.href="/login.html"
            })

            // 代码结束位置
        }
    );


</script>


<style>
    #loginFormDiv {
        margin: 100px auto;
        padding: 30px 30px 20px 50px;
        border: solid 1px blue;
        width: 600px;
        height: 450px;
        box-shadow: 5px 9px 18px 5px #eeeeee;
    }

</style>
</body>
</html>
